<template>
  <div class="login-container">
    <el-form
      ref="regForm"
      :rules="rules"
      :model="regForm"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">欢迎注册</h3>
      </div>
      <!-- 用户名 -->
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          name="username"
          v-model="regForm.username"
          placeholder="请输入账号名"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <!-- 请输入密码 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="password"
          v-model="regForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <!-- 管理员姓名 -->
      <el-form-item prop="name">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="name"
          name="name"
          v-model="regForm.name"
          placeholder="请输入姓名"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <!-- 年龄 -->
      <el-form-item prop="age">
        <span class="svg-container">
          <i class="el-icon-edit"></i>
        </span>
        <el-input
          ref="age"
          name="age"
          v-model="regForm.age"
          placeholder="请输入年龄"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <!-- 性别 -->
      <el-form-item prop="gender">
        <span class="svg-container">
          <i class="el-icon-male"></i>
        </span>
        <span style="color: #aaa; margin: 0px 20px 0px 10px">请选则性别</span>
        <el-radio v-model="regForm.gender" label="男">男</el-radio>
        <el-radio v-model="regForm.gender" label="女">女</el-radio>
      </el-form-item>
      <!-- 手机号 -->
      <el-form-item prop="phone">
        <span class="svg-container">
          <i class="el-icon-phone-outline"></i>
        </span>
        <el-input
          ref="phone"
          name="phone"
          v-model="regForm.phone"
          placeholder="请输入手机号"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <el-button
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click="regHandel"
        >确认注册</el-button
      >
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Reg",
  data() {
    // 用户名自定义验证(必须已字母开头，允许5-16字节，允许字母数字下划线)
    let usernameReg = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/;
    let validateUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空！！！"));
      }
      if (!usernameReg.test(value)) {
        callback(
          new Error("必须已字母开头，允许5-16字节，允许字母数字下划线！！！")
        );
      } else {
        callback();
      }
    };
    // 密码自定义验证(以字母开头，长度在6~18之间，只能包含字母、数字和下划线)
    let passwordReg = /^[a-zA-Z]\w{5,17}$/;
    let validatePassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("密码不能为空！！！"));
      }
      if (!passwordReg.test(value)) {
        callback(
          new Error(
            "必须以字母开头，长度在6~18之间，只能包含字母、数字和下划线！！！"
          )
        );
      } else {
        callback();
      }
    };
    // 管理员姓名
    let nameReg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/;
    let validateName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空！！！"));
      }
      if (!nameReg.test(value)) {
        callback(new Error("不能有特殊字符和数字,中文英文不能同时出现！！！"));
      } else {
        callback();
      }
    };
    // 手机号自定义验证
    let phoneReg = /^[1][3,4,5,7,8][0-9]{9}$/;
    let validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("号码不能为空！！！"));
      }
      if (!phoneReg.test(value)) {
        callback(new Error("格式有误！！！"));
      } else {
        callback();
      }
    };
    // 年龄自定义验证(18-70)
    let ageReg = /^[0-9]*$/;
    let validateAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("密码不能为空！！！"));
      }
      if (!ageReg.test(value)) {
        callback(new Error("格式不正确！！！"));
      } else {
        callback();
      }
    };
    return {
      regForm: {
        // 账号
        username: "",
        // 密码
        password: "",
        // 管理员姓名
        name: "",
        // 年龄
        age: "",
        // 性别
        gender: "",
        // 手机号
        phone: "",
        // 职位(门店管理员)
        position: "门店管理员",
        // 状态(申请中)
        status: "申请中"
      },
      // 密码是否显示密文或者明文
      passwordType: "password",
      // 校验规则
      rules: {
        // 手机号
        phone: [{ required: true, validator: validatePhone, trigger: "blur" }],
        // 用户名
        username: [
          { required: true, validator: validateUsername, trigger: "blur" }
        ],
        // 密码
        password: [
          { required: true, validator: validatePassword, trigger: "blur" }
        ],
        // 管理员姓名
        name: [{ required: true, validator: validateName, trigger: "blur" }],
        // 年龄
        age: [{ required: true, validator: validateAge, trigger: "blur" }]
      }
    };
  },
  methods: {
    // 点击切换密码是否显示明文或者密文
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    // 注册
    regHandel() {
      console.log(1212);
      this.$refs.regForm.validate(valid => {
        if (valid) {
          this.$store.dispatch("user/regActions", this.regForm).then(res => {
            if (res) {
              this.$router.push({ path: "login" });
            } else {
              this.$message({
                showClose: true,
                message: "该用户已注册！！！",
                type: "error"
              });
            }
          });
        }
      });
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
